package com.report.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Org;
import com.entity.Recharge;
import com.interfaces.OpeDao;
import com.interfaces.OrgDao;
import com.interfaces.RechargeDao;
import com.recharge.entity.JsonRecharge;
import com.recharge.srevice.RechargeService;
import com.utils.JsonResult;

/**   
* @Title: RechargeReportService.java 
* @Package com.report.service 
* @author 孙梦楠 
* @date 2017年9月18日 下午3:28:32 
* @version V1.0.0   
*/
@Service
public class RechargeReportService {
	
	
	private static final Logger log = LoggerFactory.getLogger(RechargeReportService.class);

	
	@Autowired
	private RechargeDao rechargedao;
	
	@Autowired
	private RechargeService rechargeservice;
	
	@Autowired
	private OrgDao orgdao;
	
	@Autowired
	private OpeDao opedao;
	
	//展示操作员购电报表
	@Transactional
	public JsonResult<List<JsonRecharge>> rechargereport(String issuetime1,String issuetime2,String opecode,String languagecookie)  {
	
		String direction = "购电";
		List<Recharge> recharges = rechargedao.listRechargeReport(issuetime1, issuetime2, opecode, direction);
		
		//总计报表函数
		List<JsonRecharge> rechargehistories = totelReport(languagecookie, recharges);
		
		return new JsonResult<List<JsonRecharge>>(661, "购电报表_个人", rechargehistories);
	}
	//展示操作员退费报表
	@Transactional
	public JsonResult<List<JsonRecharge>> refundreport(String issuetime1,String issuetime2,String opecode,String languagecookie)  {
		String direction = "退费";
		List<Recharge> recharges = rechargedao.listRechargeReport(issuetime1, issuetime2, opecode, direction);
		//总计报表函数
		List<JsonRecharge> rechargehistories = totelReport(languagecookie, recharges);
		return new JsonResult<List<JsonRecharge>>(663, "退费报表_个人", rechargehistories);
	}
	
	//展示组织充值报表
	//自己架构及自己以下架构的所有操作员的报表
	//展示操作员购电报表
	@Transactional
	public JsonResult<List<JsonRecharge>> rechargereport2(String issuetime1,String issuetime2,String opecode,String opeorgcookie,String languagecookie)  {
		String direction = "购电";
		List<Recharge> recharges = rechargedao.listRechargeReport(issuetime1, issuetime2, opecode, direction);
		//判断自己以下的组织架构信息
		showOrg(issuetime1, issuetime2, opeorgcookie, direction, recharges);
		//总计报表函数
		List<JsonRecharge> rechargehistories = totelReport(languagecookie, recharges);
		return new JsonResult<List<JsonRecharge>>(662, "购电报表_组织", rechargehistories);
	}
	
	@Transactional
	public JsonResult<List<JsonRecharge>> refundreport2(String issuetime1,String issuetime2,String opecode,String opeorgcookie,String languagecookie)  {
		String direction = "退费";
		List<Recharge> recharges = rechargedao.listRechargeReport(issuetime1, issuetime2, opecode, direction);
		//判断自己以下的组织架构信息
		showOrg(issuetime1, issuetime2, opeorgcookie, direction, recharges);
		//总计报表函数
		List<JsonRecharge> rechargehistories = totelReport(languagecookie, recharges);
		return new JsonResult<List<JsonRecharge>>(664, "退费报表_组织", rechargehistories);
	}
	
	//判断自己以下的组织架构信息
	private void showOrg(String issuetime1, String issuetime2, String opeorgcookie, String direction,
			List<Recharge> recharges) {
		//如果有下级架构
		if(orgdao.findByOrgfather(opeorgcookie).size()!=0) {
			List<Org> orgs=orgdao.findByOrgfather(opeorgcookie);
			for (Org org : orgs) {
				String opecode1 = opedao.findByOpeorg(Integer.valueOf(org.getOrgvalue())).getOpecode();
				List<Recharge> recharges2 = rechargedao.listRechargeReport(issuetime1, issuetime2, opecode1, direction);
				for (Recharge recharge : recharges2) {
					recharges.add(recharge);
				}
			}			
		}
	}
	
	//总计报表函数
	public List<JsonRecharge> totelReport(String languagecookie, List<Recharge> recharges)  {

		JsonRecharge rechargetotel = new JsonRecharge();

		Double moneytotel=0.00;
	
		for (Recharge recharge : recharges) {
			moneytotel=moneytotel+recharge.getMoney();
		}
		rechargetotel.setCustomeraccount("总计");
		rechargetotel.setFrequency(recharges.size());
		rechargetotel.setMoney(moneytotel);

		List<JsonRecharge> rechargehistories = rechargeservice.list_history(recharges, languagecookie);

		rechargehistories.add(rechargetotel);
		return rechargehistories;
	}
	

}
